package com.lear.rrbus.view;

import android.app.ActionBar;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.lear.rrbus.R;
import com.lear.rrbus.utils.Utils;

public class SettingsView extends ListActivity {

	private ListView refreshListView;
	private SharedPreferences prefs;

	@Override
	public void onBackPressed() {
		setResult(
				RESULT_OK,
				(new Intent()).putExtra(Utils.REFTIME,
						(refreshListView.getCheckedItemPosition() + 1) * 20));
		prefs.edit()
				.putInt(Utils.REFSELECTED,
						refreshListView.getCheckedItemPosition()).commit();
		super.onBackPressed();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		prefs = getSharedPreferences(Utils.APPNAME, Context.MODE_WORLD_READABLE);
		ActionBar mActionBar = getActionBar();
		mActionBar.setDisplayHomeAsUpEnabled(true);

		String string1;
		String string2;
		String string3;

		string1 = getBaseContext().getResources()
				.getString(R.string.menu_auto1);
		string2 = getBaseContext().getResources()
				.getString(R.string.menu_auto2);
		string3 = getBaseContext().getResources()
				.getString(R.string.menu_auto3);
		String[] options = new String[] { string1, string2, string3 };

		ListAdapter myAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_single_choice, options);

		refreshListView = getListView();
		refreshListView.setAdapter(myAdapter);
		refreshListView.setItemsCanFocus(true);
		refreshListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		int selected = prefs.getInt(Utils.REFSELECTED, -1);
		if (selected == -1) {
			refreshListView.setItemChecked(0, true);
		} else {
			refreshListView.setItemChecked(selected, true);
		}
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		switch (position) {
		case 0:
			setResult(RESULT_OK,
					(new Intent()).putExtra(Utils.REFTIME, (position + 1) * 20));
			prefs.edit().putInt(Utils.REFSELECTED, position).commit();
			break;
		case 1:
			setResult(RESULT_OK,
					(new Intent()).putExtra(Utils.REFTIME, (position + 1) * 20));
			prefs.edit().putInt(Utils.REFSELECTED, position).commit();
			break;
		case 2:
			setResult(RESULT_OK,
					(new Intent()).putExtra(Utils.REFTIME, (position + 1) * 20));
			prefs.edit().putInt(Utils.REFSELECTED, position).commit();
			break;
		default:
			break;
		}
		finish();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			setResult(RESULT_OK, (new Intent()).putExtra(Utils.REFTIME, -1));
			finish();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onRestart() {

		super.onRestart();
	}

}
